<template>
  <div class="register-container">
    <div class="register-box">
      <h2>注册</h2>
      <form @submit.prevent="handleRegister">
        <div class="form-item">
          <label>用户名</label>
          <input v-model="registerForm.username" type="text" required>
        </div>
        <div class="form-item">
          <label>邮箱</label>
          <input v-model="registerForm.email" type="email" required>
        </div>
        <div class="form-item">
          <label>密码</label>
          <input v-model="registerForm.password" type="password" required>
        </div>
        <div class="form-actions">
          <button type="submit">注册</button>
          <router-link to="/login">返回登录</router-link>
        </div>
      </form>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { userApi } from '@/api/user'
import type { RegisterData } from '@/api/user'

const router = useRouter()
const registerForm = ref<RegisterData>({
  username: '',
  email: '',
  password: ''
})

const handleRegister = async () => {
  try {
    const { data } = await userApi.register(registerForm.value)
    if (data.code === 200) {
      alert('注册成功')
      router.push('/login')
    } else {
      alert(data.message)
    }
  } catch (error) {
    console.error('注册失败:', error)
    alert('注册失败，请重试')
  }
}
</script>

<style scoped>
.register-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 20px;
}

.register-box {
  width: 100%;
  max-width: 420px;
  padding: 2.5rem;
  background: white;
  border-radius: 16px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.15);
}

h2 {
  text-align: center;
  margin-bottom: 2rem;
  color: #2d3748;
  font-size: 2rem;
}

.form-item {
  margin-bottom: 1.5rem;
}

label {
  display: block;
  margin-bottom: 0.5rem;
  color: #4a5568;
  font-weight: 500;
}

input {
  width: 100%;
  padding: 0.75rem 1rem;
  border: 2px solid #e2e8f0;
  border-radius: 8px;
  font-size: 1rem;
  transition: all 0.3s ease;
  background: #f8fafc;
}

input:focus {
  outline: none;
  border-color: #667eea;
  background: white;
  box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
}

.form-actions {
  display: flex;
  flex-direction: column;
  gap: 1rem;
  margin-top: 2rem;
}

button {
  width: 100%;
  padding: 0.875rem;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  font-size: 1rem;
  font-weight: 600;
  transition: all 0.3s ease;
}

button:hover {
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(102, 126, 234, 0.2);
}

a {
  text-align: center;
  color: #667eea;
  text-decoration: none;
  font-size: 0.95rem;
  transition: color 0.3s ease;
}

a:hover {
  color: #764ba2;
  text-decoration: underline;
}

@media (max-width: 480px) {
  .register-box {
    padding: 1.5rem;
    margin: 1rem;
    width: calc(100% - 2rem);
  }

  h2 {
    font-size: 1.75rem;
  }

  input {
    font-size: 16px;
  }
}

@media (min-width: 481px) and (max-width: 768px) {
  .register-box {
    width: 90%;
    max-width: 400px;
  }
}

@media (min-width: 769px) {
  .register-box {
    width: 420px;
  }
}

/* 添加深色模式支持 */
@media (prefers-color-scheme: dark) {
  .register-box {
    background: #1a1a1a;
  }

  h2 {
    color: #ffffff;
  }

  input {
    background: #2d3748;
    border-color: #4a5568;
    color: #ffffff;
  }

  input:focus {
    border-color: #667eea;
    background: #2d3748;
  }

  label {
    color: #e2e8f0;
  }
}
</style> 